package com.cop4331.social;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * 
 * @author Matt Szoke
 * @LastUpdated: 4/12/2012 by Matt Szoke
 * 
 * This Activity adds classes to the database
 * 
 */
public class AddclassActivity extends Activity {

	TextView temp;
	EditText coursesub;
	EditText coursenum;
	Spinner term;
	Spinner year;
	Spinner grade;
	Button add;
	Button cancel;
	Button more;

	// -----------------------------------------------
	// ONCREATE
	// -----------------------------------------------
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		// XML associated with AddclassActivity: addclass.xml
		setContentView(R.layout.addclass);

		// List contains all UCF subjects (Specific to UCF ONLY)
		String[] tempList = { "ABE", "ABT", "ABW", "ACG", "ACO", "ACR", "ADE",
				"ADV", "AEB", "AEC", "AEE", "AER", "AFA", "AFH", "AFR", "AFS",
				"AGE", "AGG", "AGR", "AHM", "AHT", "AHW", "AKA", "AKT", "AKW",
				"ALS", "AMH", "AML", "AMS", "AMT", "ANG", "ANS", "ANT", "AOM",
				"APA", "APK", "ARA", "ARC", "ARD", "ARE", "ARH", "ARR", "ART",
				"ARV", "ASC", "ASG", "ASH", "ASL", "ASN", "AST", "ATE", "ATF",
				"ATT", "AVM", "AVS", "AYM", "BAN", "BCA", "BCC", "BCH", "BCN",
				"BCT", "BCV", "BME", "BMS", "BOT", "BRC", "BSC", "BTE", "BUL",
				"CAP", "CAS", "CAT", "CBH", "CCE", "CCJ", "CDA", "CEG", "CEN",
				"CES", "CET", "CGN", "CGS", "CHD", "CHI", "CHM", "CHR", "CHS",
				"CHT", "CHW", "CIS", "CJB", "CJC", "CJD", "CJE", "CJJ", "CJK",
				"CJL", "CJT", "CLA", "CLP", "CLT", "CMC", "CMM", "CNT", "COA",
				"COE", "COM", "COP", "COS", "COT", "CPO", "CPS", "CPT", "CRW",
				"CSP", "CST", "CTE", "CTS", "CVT", "CWR", "CYP", "CZE", "CZT",
				"CZW", "DAA", "DAE", "DAN", "DAS", "DCP", "DEA", "DEC", "DEH",
				"DEM", "DEN", "DEP", "DES", "DIE", "DIG", "DIM", "DSC", "DTE",
				"DUT", "EAB", "EAP", "EAS", "EBD", "ECH", "ECM", "ECO", "ECP",
				"ECS", "ECT", "ECW", "EDA", "EDE", "EDF", "EDG", "EDH", "EDM",
				"EDP", "EDS", "EEC", "EED", "EEE", "EEL", "EER", "EES", "EET",
				"EEV", "EEX", "EGC", "EGI", "EGM", "EGN", "EGS", "EHD", "EIA",
				"EIN", "EIV", "ELD", "ELR", "EMA", "EMC", "EME", "EML", "EMR",
				"EMS", "ENC", "ENG", "ENL", "ENS", "ENT", "ENU", "ENV", "ENY",
				"EOC", "EPD", "EPH", "EPI", "ESC", "ESE", "ESI", "ESL", "EST",
				"ETC", "ETD", "ETE", "ETG", "ETI", "ETM", "ETN", "ETP", "EUH",
				"EUS", "EVI", "EVR", "EVS", "EVT", "EXP", "FAD", "FAM", "FAS",
				"FES", "FFP", "FIL", "FIN", "FLE", "FNR", "FOL", "FOR", "FOS",
				"FOT", "FOW", "FRC", "FRE", "FRT", "FRW", "FSE", "FSS", "FYC",
				"GCO", "GEA", "GEB", "GEO", "GER", "GET", "GEW", "GEY", "GFD",
				"GIS", "GLS", "GLY", "GMS", "GMT", "GMW", "GRA", "GRE", "GRG",
				"GRK", "GRW", "HAI", "HAT", "HBR", "HBT", "HCP", "HCW", "HEB",
				"HEC", "HEE", "HEV", "HFT", "HHD", "HIM", "HIN", "HIS", "HIT",
				"HIW", "HLP", "HME", "HMV", "HMW", "HNG", "HNT", "HNW", "HOE",
				"HOS", "HPS", "HRD", "HSA", "HSC", "HUM", "HUN", "HUS", "ICM",
				"ICW", "IDC", "IDD", "IDH", "IDN", "IDS", "IDT", "IDW", "IEA",
				"IHS", "IND", "INP", "INR", "INS", "INT", "IPM", "ISC", "ISM",
				"ISS", "ITA", "ITT", "ITW", "JOU", "JPN", "JPT", "JPW", "JST",
				"KOR", "KOT", "KOW", "LAA", "LAE", "LAH", "LAS", "LAT", "LAW",
				"LBS", "LDE", "LDR", "LEI", "LGL", "LGT", "LGW", "LIN", "LIS",
				"LIT", "LNW", "MAA", "MAC", "MAD", "MAE", "MAG", "MAN", "MAP",
				"MAR", "MAS", "MAT", "MCB", "MCC", "MDW", "MEA", "MEL", "MEM",
				"MET", "MGF", "MHF", "MHS", "MIS", "MKA", "MLS", "MLT", "MMC",
				"MNA", "MOB", "MOM", "MRE", "MSL", "MSS", "MTB", "MTE", "MTG",
				"MTS", "MUC", "MUE", "MUG", "MUH", "MUL", "MUM", "MUN", "MUO",
				"MUR", "MUS", "MUT", "MUY", "MVB", "MVH", "MVJ", "MVK", "MVO",
				"MVP", "MVS", "MVV", "MVW", "NEM", "NGR", "NMT", "NSC", "NSP",
				"NUR", "OCA", "OCB", "OCC", "OCE", "OCG", "OCP", "OFT", "OPT",
				"ORH", "ORI", "ORV", "OSE", "OST", "OTA", "OTH", "PAD", "PAF",
				"PAS", "PAX", "PAZ", "PCB", "PCO", "PEL", "PEM", "PEN", "PEO",
				"PEP", "PEQ", "PET", "PGY", "PHA", "PHC", "PHH", "PHI", "PHM",
				"PHP", "PHT", "PHY", "PHZ", "PKG", "PLA", "PLP", "PLS", "PLT",
				"PLW", "PMA", "PMT", "POL", "POR", "POS", "POT", "POW", "PPE",
				"PRN", "PRO", "PRT", "PSB", "PSC", "PSE", "PSG", "PSY", "PTA",
				"PTN", "PUP", "PUR", "QMB", "RAT", "RCS", "REA", "RED", "REE",
				"REL", "RET", "RHT", "RMI", "ROT", "RSD", "RTE", "RTT", "RTV",
				"RUS", "RUT", "RUW", "SAL", "SBM", "SCA", "SCC", "SCE", "SCT",
				"SCW", "SCY", "SDS", "SEC", "SED", "SER", "SHO", "SHT", "SLA",
				"SLL", "SLS", "SLT", "SLW", "SMT", "SNW", "SON", "SOP", "SOS",
				"SOW", "SPA", "SPB", "SPC", "SPM", "SPN", "SPS", "SPT", "SPW",
				"SRK", "SRT", "SRW", "SSA", "SSE", "SST", "SSW", "STA", "STS",
				"SUR", "SVL", "SWA", "SWS", "SWT", "SWW", "SYA", "SYD", "SYG",
				"SYO", "SYP", "TAR", "TAX", "TCN", "TDR", "THE", "TPA", "TPP",
				"TRA", "TSL", "TTE", "TUR", "TUT", "TUW", "URP", "URS", "VAR",
				"VEC", "VEM", "VIC", "VME", "VPI", "VTN", "VTT", "VTW", "WCL",
				"WDS", "WIS", "WOH", "WOL", "WOT", "WOW", "WST", "XHO", "XHT",
				"XHW", "YOR", "YOT", "YRW", "ZOO" };

		// Store classes in a List Object
		final List<String> courses = Arrays.asList(tempList);

		// EditText Fields
		coursesub = (EditText) findViewById(R.id.tf_coursesub); // Allows max of
																// 3 letter
																// input
		coursenum = (EditText) findViewById(R.id.tf_coursenum); // Allows max of
																// 4 digit input

		// Spinner fields
		term = (Spinner) findViewById(R.id.s_term); // Contains terms Spring
													// Fall Summer
		year = (Spinner) findViewById(R.id.s_year); // Contains years from 2000
													// to 2012
		grade = (Spinner) findViewById(R.id.s_grade); // Contains letter grades
														// A-F

		// Buttons
		add = (Button) findViewById(R.id.b_add); // Finish and add class
		more = (Button) findViewById(R.id.b_more); // Add more classes
		cancel = (Button) findViewById(R.id.b_cancel); // Cancel

		// TextView -- Temp holds error output to display to User
		temp = (TextView) findViewById(R.id.tv_temp);

		// Retreive Username from SQLite Database (In Android DB)
		DatabaseHandler db = new DatabaseHandler(getApplicationContext());
		HashMap<String, String> user = db.getUserDetails();
		final String un = user.get("name");

		// TitleBar Buttons
		ImageButton about, home, message;

		about = (ImageButton) findViewById(R.id.b_about);
		home = (ImageButton) findViewById(R.id.b_home);
		message = (ImageButton) findViewById(R.id.b_message);

		UserFunctions uf = new UserFunctions();

		// Check for new messages
		JSONObject j_mess = uf.retreiveMessages(un, "new");

		try {
			if (Integer.parseInt(j_mess.getString("size")) > 0) {
				message.setVisibility(View.VISIBLE);
			} else
				message.setVisibility(View.GONE);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		about.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Dialog dialog = new Dialog(AddclassActivity.this);

				dialog.setContentView(R.layout.help_dialog);
				dialog.setTitle("CramSessions v 1.0 2012");
				dialog.setCancelable(true);

				// Create and show a dialog to help the user with this specific
				// field
				TextView text = (TextView) dialog.findViewById(R.id.text);
				text.setText("CramSessions was created by:\n\n-Matt Szoke\n-Daniel Moody\n-Jeovanny Alvarez\n-Kia Manoochehri\n-Rj Anderson\n-Doug Wagner");
				text.setGravity(Gravity.LEFT);
				dialog.setCanceledOnTouchOutside(true);
				dialog.show();
			}
		});

		// Return to dasboard
		home.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Intent sa = new Intent(getApplicationContext(),
						DashBoardActivity.class);
				startActivity(sa);
				finish();

			}
		});

		// Goto messages
		message.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Intent sa = new Intent(getApplicationContext(),
						MessageNotifications.class);
				startActivity(sa);
				finish();

			}
		});

		// Add Functionality to "FINISH" Button
		add.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				// Retreive fields
				String csub = coursesub.getText().toString();
				String cnum = coursenum.getText().toString();
				String cterm = term.getSelectedItem().toString();
				String cyear = year.getSelectedItem().toString();
				String cgrade = grade.getSelectedItem().toString();

				// Create Coursename which is Subject + the CourseNumber (i.e
				// COP3223)
				String coursename = csub + "" + cnum;

				// Create CourseYear which is the Term + Year (i.e Fall2012)
				String courseyear = cterm + "" + cyear;

				UserFunctions userFunction = new UserFunctions();

				// String checking -- NEEDS EXPANSION
				if (csub.length() != 3) {
					temp.setText("Please enter a subject.");
				} else if (!courses.contains(csub)) {
					temp.setText("Your subject does not exist.");
				} else if (cnum.length() != 4) {
					temp.setText("Please enter a course number.");
				} else {

					// Send data to MYSQL Database using UserFunction Class
					// class database stores (cname - (coursename), username -
					// (un), term - (courseyear), tutoring - (1 for active, 0
					// for not), grade - (cgrade))
					JSONObject json = userFunction.addClass(coursename, un,
							courseyear, cgrade);

					// json Object stores 1 value, which is success. If success
					// is not equal to null, SQL insert worked.
					try {
						if (json.getString("success") != null) {

							// Make sure error view is removed from vision of
							// the user.
							temp.setText("");
							String res = json.getString("success");

							// Success == 1 if SQL insert, inserted correctly
							if (Integer.parseInt(res) == 1) {

								// We are done, return to viewprofile
								Intent vp = new Intent(getApplicationContext(),
										ViewProfileActivity.class);
								startActivity(vp);
								finish();

							} else
								temp.setText("Entry already exists.");
						}

					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}
		});

		// Add functionality to "ADD ANOTHER COURSE" button
		more.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				// Retreive fields
				String csub = coursesub.getText().toString();
				String cnum = coursenum.getText().toString();
				String cterm = term.getSelectedItem().toString();
				String cyear = year.getSelectedItem().toString();
				String cgrade = grade.getSelectedItem().toString();

				// Create Coursename which is Subject + the CourseNumber (i.e
				// COP3223)
				String coursename = csub + "" + cnum;

				// Create CourseYear which is the Term + Year (i.e Fall2012)
				String courseyear = cterm + "" + cyear;

				UserFunctions userFunction = new UserFunctions();

				// String checking -- NEEDS EXPANSION
				if (csub.length() != 3) {
					temp.setText("Please enter a subject.");
				} else if (!courses.contains(csub)) {
					temp.setText("Your subject does not exist.");
				} else if (cnum.length() != 4) {
					temp.setText("Please enter a course number.");
				} else {

					// Send data to MYSQL Database using UserFunction Class
					// class database stores (cname - (coursename), username -
					// (un), term - (courseyear), tutoring - (1 for active, 0
					// for not), grade - (cgrade))
					JSONObject json = userFunction.addClass(coursename, un,
							courseyear, cgrade);

					// json Object stores 1 value, which is success. If success
					// is not equal to null, SQL insert worked.
					try {
						if (json.getString("success") != null) {

							// Make sure error view is removed from vision of
							// the user.
							temp.setText("");
							String res = json.getString("success");

							// Success == 1 if SQL insert, inserted correctly
							if (Integer.parseInt(res) == 1) {

								// We are done, repeat activity to add another
								// class
								Intent addc = new Intent(
										getApplicationContext(),
										AddclassActivity.class);
								startActivity(addc);
								finish();

							} else
								temp.setText("Entry already exists.");
						}

					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}
		});

		// Add functionality to "CANCEL" button
		cancel.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				// Do not add anything, return to viewprofile
				Intent vp = new Intent(getApplicationContext(),
						ViewProfileActivity.class);
				startActivity(vp);
				finish();

			}
		});

	}

}
